#!/usr/bin/make

# build only FOODIE library (static)

# defaults
COMPILER = gnu

# main building variables
ifeq "$(COMPILER)" "gnu"
  FC    = gfortran
  OPTSC = -cpp -c -frealloc-lhs -O2  -J $(DMOD)
  OPTSL = -J $(DMOD)
endif
ifeq "$(COMPILER)" "intel"
  FC    = ifort
  OPTSC = -c -O2 -module $(DMOD)
  OPTSL = -module $(DMOD)
endif
ifeq "$(COMPILER)" "ibm"
  FC    = bgxlf2008_r
  OPTSC = -c -O2 -qmoddir=$(DMOD) -I$(DMOD)
  OPTSL = -qmoddir=$(DMOD) -I$(DMOD)
endif
ifeq "$(COMPILER)" "cray"
  FC    = ftn
  OPTSC = -c -O 0 -e Z -g
endif
DSRC    = src/lib
DBLD    = build/lib/
DOBJ    = $(DBLD)obj/
DMOD    = $(DBLD)mod/
VPATH   = $(DSRC) $(DOBJ) $(DMOD)
MKDIRS  = $(DBLD) $(DOBJ) $(DMOD)

#auxiliary variables
COTEXT  = "Compiling $(<F)"
LITEXT  = "Assembling $@"

#building rules
foodie: $(MKDIRS) $(DOBJ)foodie.o
	@echo $(LITEXT)
	@ar -rcs $(DBLD)libfoodie.a $(DOBJ)*.o ; ranlib $(DBLD)libfoodie.a

#compiling rules
$(DOBJ)foodie_integrator_backward_differentiation_formula.o: src/lib/foodie_integrator_backward_differentiation_formula.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_adt_integrand.o: src/lib/foodie_adt_integrand.f90 \
	$(DOBJ)foodie_kinds.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_integrator_tvd_runge_kutta.o: src/lib/foodie_integrator_tvd_runge_kutta.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_integrator_euler_explicit.o: src/lib/foodie_integrator_euler_explicit.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_kinds.o: src/lib/foodie_kinds.f90
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_integrator_leapfrog.o: src/lib/foodie_integrator_leapfrog.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_integrator_adams_bashforth.o: src/lib/foodie_integrator_adams_bashforth.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_utils.o: src/lib/foodie_utils.f90 \
	$(DOBJ)foodie_kinds.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_integrator_adams_moulton.o: src/lib/foodie_integrator_adams_moulton.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_integrator_low_storage_runge_kutta.o: src/lib/foodie_integrator_low_storage_runge_kutta.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie.o: src/lib/foodie.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_integrator_adams_bashforth.o \
	$(DOBJ)foodie_integrator_adams_bashforth_moulton.o \
	$(DOBJ)foodie_integrator_adams_moulton.o \
	$(DOBJ)foodie_integrator_backward_differentiation_formula.o \
	$(DOBJ)foodie_integrator_embedded_runge_kutta.o \
	$(DOBJ)foodie_integrator_euler_explicit.o \
	$(DOBJ)foodie_integrator_leapfrog.o \
	$(DOBJ)foodie_integrator_low_storage_runge_kutta.o \
	$(DOBJ)foodie_integrator_tvd_runge_kutta.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_integrator_embedded_runge_kutta.o: src/lib/foodie_integrator_embedded_runge_kutta.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

$(DOBJ)foodie_integrator_adams_bashforth_moulton.o: src/lib/foodie_integrator_adams_bashforth_moulton.f90 \
	$(DOBJ)foodie_adt_integrand.o \
	$(DOBJ)foodie_kinds.o \
	$(DOBJ)foodie_integrator_adams_bashforth.o \
	$(DOBJ)foodie_integrator_adams_moulton.o \
	$(DOBJ)foodie_utils.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC)  $< -o $@

#phony auxiliary rules
.PHONY : $(MKDIRS)
$(MKDIRS):
	@mkdir -p $@
.PHONY : cleanobj
cleanobj:
	@echo deleting objects
	@rm -fr $(DOBJ)
.PHONY : cleanmod
cleanmod:
	@echo deleting mods
	@rm -fr $(DMOD)
.PHONY : clean
clean: cleanobj cleanmod
.PHONY : cleanall
cleanall: clean cleanexe
